import {Util} from "../utils/Util";
import {InputStr, RewardType} from "../type/Type";
import {ConfigConstant, ConfigKey} from "../constant/ConfigConstant";
import {GlobalEnv} from "../utils/GlobalEnv";
import {DataRouter} from "../frame/dataSource/DataRouter";
import {TaskController} from "../controller/TaskController";
// @ts-ignore
import {decrypted} from "../utils/decrypt";
import * as jsonConfig from "../constant/jsonConfig.json";
import {AttributeEnum} from "../frame/dataSource/DataModel";
import {StoreController} from "../controller/StoreController";
import {gameTypePolicy} from "../policy/GameTypePolicy";
import {TriggerFactory} from "../frame/TriggerFactory";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {PlayerDataKey} from "../frame/dataSource/DataSource";
import {Logger} from "../frame/apiModel/config/Logger";
import {ModelFactory} from "../frame/apiModel/adapterModel/model5/ModelFactory";
import {ItemJson} from "../frame/apiModel/model/StoreJson";
import Configer from "../frame/apiModel/config/Configer";
import {
    BroadcastRequest,
    BroadcastType, ControllerNo,
    GuiControllerOpt,
    GuiId,
    TimeControllerOpt
} from "../controller/model/ControllerOpt";
import {TimeController} from "../controller/TimeController";
import {GuiController, OptType} from "../controller/GuiController";
import {InputUtil} from "../utils/InputUtil";
import MathUtil from "../frame/codeAdapter/MathUtil";
import {GlobalModel} from "../frame/apiModel/adapterModel/model1/GlobalModel";
import {UnitController} from "../controller/UnitController";

export function inputTrigger() {
    const onInput = function (this: void, player: PlayerModel, str: string) {
        const arr = CodeUtil.getArrBySplit(str);
        if (arr) {
            const action = arr[0];
            const name = arr[1];
            const value = arr[2];
            let lowerAction;
            if (action) {
                lowerAction = action.toLowerCase();
            }
            switch (lowerAction) {
                // case 'pase': {
                //     const attackTimer = GlobalEnv.waveTimer;
                //     Logger.toastProduct('怪物停止进攻')
                //     attackTimer.pause();
                //     const canelCallBack = () => {
                //         attackTimer.resume();
                //     }
                //     const timerOpt = new TimeControllerOpt(undefined, canelCallBack, 1);
                //     TimeController.getInstance().startTimer({ time: 30, opt: timerOpt });
                //     break;
                // }
                case InputStr.blacklist.toLowerCase(): {
                    player.data[PlayerDataKey.bBlacklist] = 1;
                    Logger.toastProduct('已经将自己加入黑名单，黑名单中的玩家无法获得任何商城道具', player)
                    return;
                }
                case InputStr.whitelist.toLowerCase(): {
                    player.data[PlayerDataKey.bWhitelist] = 1;
                    Logger.toastProduct('已经将自己加入白名单，白名单中的玩家会获得所有商城道具', player)
                    const opt = new GuiControllerOpt(GuiId.adminBar, { });
                    GuiController.getInstance().showGui(opt, player, OptType.open);
                    return;
                }
                case InputStr.hg.toLowerCase(): {
                    const useInputBackHome = Configer.getConfig(ConfigKey.useInputBackHome);
                    if (!CodeUtil.isNullOrZero(useInputBackHome)) {
                        UnitController.getInstance().backHome(player);
                    }
                    return;
                }
            }
            switch (action) {
                case InputStr.uuid: {
                    const uuid = player.data[PlayerDataKey.uuid];
                    Logger.toastProduct(`您本局的uuid为: ${uuid}`, player)
                    return;
                }
                case InputStr.userId: {
                    const userId = player.data[PlayerDataKey.userId];
                    Logger.toastProduct(`您当前账号的userId为: ${userId}`, player)
                    return;
                }
                case InputStr.subCarmeraHeight: {
                    const cameraUnit: number = Configer.getConfig(ConfigKey.cameraUnit);
                    player.addCarmeraHeightFun(-cameraUnit);
                    break;
                }
                case InputStr.addCarmeraHeight: {
                    const cameraUnit: number = Configer.getConfig(ConfigKey.cameraUnit);
                    player.addCarmeraHeightFun(cameraUnit);
                    break;
                }
                case InputStr.compensateDecodeUserid:
                case InputStr.compensateDecodeUuid: {
                    const encode = name;
                    let uuid;
                    if (action === InputStr.compensateDecodeUserid) {
                        uuid = player.data[PlayerDataKey.userId];
                    } else if (action === InputStr.compensateDecodeUuid) {
                        uuid = player.data[PlayerDataKey.uuid];
                    }
                    const str = decrypted(jsonConfig.encryptKeys, encode, jsonConfig.encryptSplit, jsonConfig.indexSplit);
                    const bodyArr = CodeUtil.getArrBySplit(str);
                    if (bodyArr[0] === uuid) {
                    } else {
                        Logger.toastProduct(`请不要输入其他人的兑换码`, player)
                        return;
                    }
                    const reward = bodyArr[1];
                    if (reward) {
                        const hero = GlobalEnv.getHero(player);
                        const ret = TaskController.getInstance().reward(hero, hero, RewardType.bag, reward);
                        if (ret) {
                            if (action === InputStr.compensateDecodeUserid) {
                                Logger.toastProduct(`兑换成功`, player)
                            } else if (action === InputStr.compensateDecodeUuid) {
                                player.reflushUUId();
                                Logger.toastProduct(`兑换成功，更新UUID`, player)
                            }
                        } else {
                            Logger.toastProduct(`兑换失败，请不要频繁尝试，以免损坏存档！`, player)
                        }

                    } else {
                        Logger.toastProduct(`兑换码不正确`, player)
                    }
                    return;
                }
                case InputStr.changeAction: {
                    if (name === InputStr.mode) {
                        const num = MathUtil.toNumber(value);
                        ConfigConstant[ConfigKey.mode] = num;
                        Logger.toastProduct(`ConfigConstant${name} 设置为 ${Configer.getConfig(ConfigKey.mode)}`)
                    } else if (name === InputStr.saveMode) {
                        const num = MathUtil.toNumber(value);
                        ConfigConstant[ConfigKey.saveMode] = num;
                        Logger.toastProduct(`修改为存储mode ${num} 的日志`)
                    }
                    break;
                }
                case InputStr.closeAction: {
                    if (name === InputStr.gui) {
                        const opt = new GuiControllerOpt(value, { });
                        GuiController.getInstance().showGui(opt, player, OptType.close);
                    }
                    break;
                }
                case InputStr.checkAction: {
                    const hero = GlobalEnv.getHero(player);
                    const life = Util.getTargetAttributeEnumByNotHero(hero, AttributeEnum.life)
                    Logger.toastProduct(`角色目前生命值为${life}`, player)
                    return;
                }
                case InputStr.getAction: {
                    InputUtil.doActionByStr(player, name, value)
                    return;
                }
                case InputStr.clearAction: {
                    if (name) {
                        if (name === InputStr.lockKey) {
                            TaskController.getInstance().clearLockKey(player);
                        } else {
                            TaskController.getInstance().clearStoreKey(player, name);
                        }
                    } else {
                        const saveControllerNos = gameTypePolicy.getSaveControllerNos();
                        StoreController.getInstance().getStore(player, saveControllerNos, true);
                    }
                    return;
                }
            }
        }
        const inputDatas = DataRouter.getInputDatas();
        for (let i = 0; i < inputDatas?.length; i++) {
            const inputData = inputDatas[i];
            const command: string = inputData.command;
            const times = inputData.times;
            if (str) {
                if (`i${str}` === `i${command}`) {
                    if (!CodeUtil.isNullOrZero(times)) {
                        const ret = GlobalEnv.addInputNum(player, command, times);
                        if (ret) {
                        } else {
                            Logger.toastProduct('兑换次数已达到最大！', player)
                            return;
                        }
                    }
                    const rewardId = inputData.reward;
                    const hero = GlobalEnv.getHero(player);
                    TaskController.getInstance().reward(hero, hero, RewardType.bag, rewardId)
                    const text = inputData.text;
                    if (text) {
                        Logger.toastProduct(text, player)
                    }
                    return;
                }
            }
        }
        GlobalModel.addChat(player.name, str);
    }
    TriggerFactory.getInstance().registerPlayerInput(onInput)
}
